﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary;

namespace ControlLibrary
{
    public partial class ListBoxAddSup : UserControl
    {
        public delegate void ItemAdded(object sender, string title);
        public event ItemAdded itemAdded = null;

        public delegate void ItemDeleted(object sender, int iIdx, string title);
        public event ItemDeleted itemDeleted = null;

        public delegate void ItemChanged(object sender, string title);
        public event ItemChanged itemChanged = null;

        public delegate void ItemTitleModified(object sender, int iIdx, string oNewtitle, string oOldTitle);
        public event ItemTitleModified itemTitleModified = null;

        public delegate void ItemMoved(object sender, int idxOrigine, int idxDestination);
        public event ItemMoved itemMoved = null;

        private int _selectedIndex;
        private string _defaultItemTitle;
        private Color _textBoxColor = Color.Black;
        private string _disabledText;
        private bool VisibleAddDelete;
        private bool VisibleMove;
        private bool _newItem;

        public ListBoxAddSup()
        {
            InitializeComponent();
            init();
            _defaultItemTitle = "Item n°";
        }

        public void init()
        {
            listBox1.Items.Clear();
            textBox1.Clear();
            textBox1.Text = _disabledText;

            textBox1.Font = new Font("Microsoft Sans Serif", (float)8.25, FontStyle.Italic);
            textBox1.Enabled = false;

            button_Del.Enabled = false;
            button_Add.Enabled = true;
            button_Down.Enabled = false;
            button_Up.Enabled = false;
        }

        # region GET/SET

        public string ToolTip_ButtonUP
        {
            set { toolTip1.SetToolTip(button_Up, value); }
            get { return toolTip1.GetToolTip(button_Up); }
        }
        
        public string ToolTip_ButtonDown
        {
            set { toolTip1.SetToolTip(button_Down, value); }
            get { return toolTip1.GetToolTip(button_Down); }
        }
        
        public string ToolTip_ButtonAdd
        {
            set { toolTip1.SetToolTip(button_Add, value); }
            get { return toolTip1.GetToolTip(button_Add); }
        }
        
        public string ToolTip_ButtonDel
        {
            set { toolTip1.SetToolTip(button_Del, value); }
            get { return toolTip1.GetToolTip(button_Del); }
        }

        public string DefaultItemTitle
        {
            get {return _defaultItemTitle;}
            set{_defaultItemTitle = value;}
        }

        public bool _VisibleAddDelete
        {
            get { return VisibleAddDelete; }
            set
            {
                button_Del.Visible = value;
                button_Add.Visible = value;
                VisibleAddDelete = value;
            }
        }
        public bool _VisibleMove
        {
            get { return VisibleMove; }
            set
            {
                button_Down.Visible = value;
                button_Up.Visible = value;
                VisibleMove = value;
            }
        }

        public int nbItem
        {
            get { return listBox1.Items.Count; }
        }

        public string selectedItem
        {
            get { return listBox1.SelectedItem.ToString(); }
        }

        public int selectedIndex
        {
            get 
            { 
                return _selectedIndex; 
            }
            set 
            {
                if (value >= 0 && value < listBox1.Items.Count)
                {
                    listBox1.SelectedIndex = value;
                    _selectedIndex = value;
                }
            }
        }

        public bool getNameByIndex(int index, out string oName)
        {
            bool RC = true;
            oName = string.Empty;
            if (index >= 0 && index < listBox1.Items.Count)
                oName = listBox1.Items[index].ToString();
            else RC = false;

            return RC;
        }

        public bool getLastItem(out string oItem)
        {
            bool RC = true;
            oItem = string.Empty;

            int idx = listBox1.Items.Count - 1;
            if (idx < listBox1.Items.Count && idx >= 0)
                oItem = listBox1.Items[idx].ToString();
            else RC = false;

            return RC;
        }

        public void setDesabledText(string iText)
        {
            _disabledText = iText;
            listBox1.Text = iText;
        }

        /*public void setDefaultItemTitle(string iText)
        {
            _defaultItemTitle = iText;
        }*/

        public void setEnable(bool iEnable)
        {
            this.button_Del.Enabled = iEnable;
            this.button_Add.Enabled = iEnable;
            this.textBox1.Enabled = iEnable;
        }

        /*public bool setEnable(bool iEnable, Control iControl)
        {
            bool RC = true;

            if ("textbox1" == iControl.ToString())
                this.textBox1.Enabled = iEnable;
            else if ("buttonPlus" == iControl.ToString())
                this.button_Plus.Enabled = iEnable;
            else if ("buttonMoins" == iControl.ToString())
                this.button_Moins.Enabled = iEnable;
            else if ("buttonUp" == iControl.ToString())
                this.button_Up.Enabled = iEnable;
            else if ("buttonDown" == iControl.ToString())
                this.button_Down.Enabled = iEnable;
            else RC = false;

            //TraceServices.writeIf(!RC, "ListBoxAddSup", "setEnable", "iControl non renconnu");
            return RC;
        }*/

        public void setToolTipPlus(string iToolTip)
        {
            toolTip1.SetToolTip(button_Add, iToolTip);
        }

        public void setToolTipMoins(string iToolTip)
        {
            toolTip1.SetToolTip(button_Del, iToolTip);
        }

        public void setTextBoxFontColor(System.Drawing.Color iColor)
        {
            textBox1.ForeColor = iColor;
            _textBoxColor = iColor;
        }

        public List<string> Items
        {
            get
            {
                List<string> res = new List<string>();
                foreach (object obj in listBox1.Items)
                    res.Add(obj.ToString());
                return res;
            }
        }

        #endregion

        public void addItem(string iTitle)
        {
            if (false == textBox1.Enabled)
            {
                textBox1.Enabled = true;
                textBox1.Font = new Font("Microsoft Sans Serif", (float)8.25, FontStyle.Regular);
                textBox1.ForeColor = _textBoxColor;
            }

            string itemTitle;
            if (null != iTitle && string.Empty != iTitle)
                itemTitle = iTitle;
            else
                itemTitle = _defaultItemTitle + (listBox1.Items.Count + 1);

            //Check if itemTitle already exist
            if (listBox1.Items.Count > 0)
            {
                int idx = listBox1.Items.IndexOf(itemTitle);
                int i = 1;
                while (idx != -1)
                {
                    itemTitle = _defaultItemTitle + (listBox1.Items.Count + 1 + i);
                    idx = listBox1.Items.IndexOf(itemTitle);
                }
            }

            listBox1.Items.Add(itemTitle);
            setEnable(true);
        }

        public void addItem()
        {
            addItem(null);
        }

        public void clearItem()
        {
            listBox1.Items.Clear();
            textBox1.Text = string.Empty;
            textBox1.Enabled = false;
            _selectedIndex = listBox1.SelectedIndex;
        }

        public void clearSelected()
        {
            listBox1.ClearSelected();
        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            addItem(null);
            string itemTitleAdded;
            bool RC = getLastItem(out itemTitleAdded);

            if (true == RC)
            {
                //Envoie de l'evenement ItemAdded
                if (null != itemAdded)
                    itemAdded(this, itemTitleAdded);

                //Mise à jour de l'index selectionné (donc envoie de l'evenement itemChanged)
                listBox1.SelectedIndex = listBox1.Items.Count - 1;
                _selectedIndex = listBox1.SelectedIndex;

                //Quand un nouvel item est ajouté, mettre le contenu du textBox en surbrillance
                textBox1.Focus();
                textBox1.SelectAll();

                _newItem = true;
            }
        }

        private void button_Del_Click(object sender, EventArgs e)
        {
            if (_selectedIndex >= 0 && _selectedIndex < listBox1.Items.Count)
            {
                string selectedItem = listBox1.Items[_selectedIndex].ToString();
                if (DialogResult.Yes == MessageBox.Show(this, Parameters.Values.RM.GetString("FP_Categories_Del")  +" " +selectedItem + " ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    int idx = _selectedIndex;
                    listBox1.Items.RemoveAt(_selectedIndex);

                    _selectedIndex = idx;
                    if (idx < 0)
                    {
                        if (listBox1.Items.Count > 0)
                        {
                            _selectedIndex = 0;
                            listBox1.SelectedIndex = _selectedIndex;
                        }
                        else init();
                    }
                    else
                    {
                        if (listBox1.Items.Count <= _selectedIndex)
                            _selectedIndex = listBox1.Items.Count - 1;

                        listBox1.SelectedIndex = _selectedIndex;
                    }
                    if (nbItem == 0) init();
                    if (null != itemDeleted)
                        itemDeleted(this, idx, selectedItem);
                }
            }
        }

        private void button_Up_Click(object sender, EventArgs e)
        {
            if (_selectedIndex > 0 && _selectedIndex < listBox1.Items.Count)
            {
                int newIndex = _selectedIndex - 1;
                string item = listBox1.Items[_selectedIndex].ToString();
                if (newIndex >= 0 && newIndex < listBox1.Items.Count)
                {
                    listBox1.Items.RemoveAt(_selectedIndex);
                    listBox1.Items.Insert(newIndex, item);
                    listBox1.SelectedIndex = newIndex;
                    if (null != itemMoved)
                        itemMoved(this, newIndex + 1, newIndex);
                }
                listBox1.Focus();
            }
        }

        private void button_Down_Click(object sender, EventArgs e)
        {
            if (_selectedIndex >= 0 && _selectedIndex < listBox1.Items.Count - 1)
            {
                int newIndex = _selectedIndex + 1;
                string item = listBox1.Items[_selectedIndex].ToString();
                if (newIndex > 0 && newIndex < listBox1.Items.Count)
                {
                    listBox1.Items.RemoveAt(_selectedIndex);
                    listBox1.Items.Insert(newIndex, item);
                    listBox1.SelectedIndex = newIndex;
                    if (null != itemMoved)
                        itemMoved(this, newIndex - 1, newIndex);
                }
                listBox1.Focus();
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                string newTitle;
                string oldTitle = listBox1.SelectedItem.ToString();
                bool modified;
                itemNameModification(out newTitle, out modified);

                if (modified && null != itemTitleModified)
                    itemTitleModified(this, _selectedIndex, newTitle, oldTitle);
            }
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            string newTitle;
            string oldTitle = listBox1.SelectedItem.ToString();
            bool modified;
            itemNameModification(out newTitle, out modified);
            if (modified && null != itemTitleModified)
                itemTitleModified(this, _selectedIndex, newTitle, oldTitle);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            _newItem = false;
            _selectedIndex = listBox1.SelectedIndex;
            if (_selectedIndex < listBox1.Items.Count && _selectedIndex >= 0)
            {
                string text = listBox1.SelectedItem.ToString();
                textBox1.Text = text;
                if (null != itemChanged)
                    itemChanged(this, text);
                if (listBox1.Items.Count > 1)
                {
                    if (_selectedIndex == 0)
                    {
                        button_Up.Enabled = false;
                        button_Down.Enabled = true;
                    }
                    else if (_selectedIndex == listBox1.Items.Count - 1)
                    {
                        button_Up.Enabled = true;
                        button_Down.Enabled = false;
                    }
                    else
                    {
                        button_Up.Enabled = true;
                        button_Down.Enabled = true;
                    }
                }
                else
                {
                    button_Up.Enabled = false;
                    button_Down.Enabled = false;
                }
            }
        }

        private void itemNameModification(out string oNewTitle, out bool modified)
        {
            //Y'a-t-il eu modification du texte ?
            string currentText = textBox1.Text;
            modified = false;
            oNewTitle = currentText;
            if (_selectedIndex >= 0 && _selectedIndex < listBox1.Items.Count)
            {
                string selectedText = listBox1.Items[_selectedIndex].ToString();

                if (0 != currentText.CompareTo(selectedText)) //Text modifié
                {
                    //Texte déjà existant dans la liste ?
                    int nbItems = listBox1.Items.Count;
                    string name;
                    bool exist = false;
                    for (int i = 0; i < nbItems && false == exist; i++)
                    {
                        name = listBox1.Items[i].ToString();
                        if (0 == currentText.CompareTo(name))
                            exist = true;
                    }

                    if (false == exist)
                    {
                        int selectedIndex = _selectedIndex;
                        
                        if (_newItem || DialogResult.Yes == MessageBox.Show(this, Parameters.Values.RM.GetString("FP_Categorie_Rename") + " \"" + selectedText + "\" ?", "", MessageBoxButtons.YesNo))
                        {
                            listBox1.Items.RemoveAt(selectedIndex);
                            listBox1.Items.Insert(selectedIndex, currentText);
                            oNewTitle = currentText;
                            listBox1.SelectedIndex = selectedIndex;
                            _selectedIndex = selectedIndex;
                            modified = true;
                        }
                        else
                        {
                            textBox1.Text = selectedText;
                            oNewTitle = selectedText;
                            modified = false;
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, Parameters.Values.RM.GetString("FP_Categorie_Rename2"), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        listBox1.SelectedIndex = _selectedIndex;
                        textBox1.Text = listBox1.SelectedItem.ToString();
                        modified = false;
                    }
                }
            }
        }

        private void button_Add_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            button_Add.Image = Properties.Resources.AddR;
            button_Add.FlatAppearance.BorderSize = 1;
            button_Add.FlatAppearance.BorderColor = Color.Red;
        }

        private void button_Add_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            button_Add.Image = Properties.Resources.Add;
            button_Add.FlatAppearance.BorderSize = 0;
        }

        private void button_Del_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            button_Del.Image = Properties.Resources.DelR;
            button_Del.FlatAppearance.BorderSize = 1;
            button_Del.FlatAppearance.BorderColor = Color.Red;
        }

        private void button_Del_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            button_Del.Image = Properties.Resources.Del;
            button_Del.FlatAppearance.BorderSize = 0;
        }

        private void button_Up_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            button_Up.Image = Properties.Resources.UpR;
            button_Up.FlatAppearance.BorderSize = 1;
            button_Up.FlatAppearance.BorderColor = Color.Red;
        }

        private void button_Down_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            button_Down.Image = Properties.Resources.Down;
            button_Down.FlatAppearance.BorderSize = 0;
        }

        private void button_Up_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
            button_Up.Image = Properties.Resources.Up;
            button_Up.FlatAppearance.BorderSize = 0;
        }

        private void button_Down_MouseEnter(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Hand;
            button_Down.Image = Properties.Resources.DownR;
            button_Down.FlatAppearance.BorderSize = 1;
            button_Down.FlatAppearance.BorderColor = Color.Red;
        }
    }
}
